package com.lajiaomian.springbootruijiwaimai.service.impl.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lajiaomian.springbootruijiwaimai.common.MyException;
import com.lajiaomian.springbootruijiwaimai.mapper.CategoryMapper;
import com.lajiaomian.springbootruijiwaimai.pojo.Category;
import com.lajiaomian.springbootruijiwaimai.service.impl.CategoryService;
import com.lajiaomian.springbootruijiwaimai.service.impl.DishService;
import com.lajiaomian.springbootruijiwaimai.service.impl.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @author lajiaomian
 * @create 2022-08-12-22:37
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public Page getCategoryPage(Integer page, Integer pageSize) {
        Page<Category> pageIfo = new Page<>(page,pageSize);
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("sort");
        return page(pageIfo,wrapper);
    }

    @Override
    public void remove(Long id) {
        Long dishId = dishService.CountCategoryId(id);
        Long setmealId = setmealService.CountCategoryId(id);
        //如果dish表中存在关联的categoryId
        if(dishId > 0){
            throw new MyException("存在关联的dish!");
        }
        //如果setmeal表中存在关联的categoryId
        if (setmealId > 0){
            throw  new MyException("存在关联的setmealId!");
        }
        removeById(id);
    }


}
